﻿using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using RBAC.Api.Erroc;
using RBAC.Api.Writh.Command.ContentManagement;

namespace RBAC.Api.Writh.Controllers.ContentManagement
{
    /// <summary>
    /// 轮播图表管理控制器--写
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class CarouselChartController : ControllerBase
    {
        /// <summary>
        /// 中介者
        /// </summary>
        private readonly IMediator mediator;

        /// <summary>
        /// 日志记录器
        /// </summary>
        private readonly ILogger<CarouselChartController> logger;

        public CarouselChartController(IMediator mediator, ILogger<CarouselChartController> logger)
        {
            this.mediator = mediator;
            this.logger = logger;
        }
        /// <summary>
        /// 添加轮播图表
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<object>> AddAddCarouselChart(AddCarouselChartCommand command)
        {
            try
            {
                logger.LogInformation("开始添加轮播图表，命令参数：{@Command}", command);
                var result = await mediator.Send(command);
                logger.LogInformation("添加轮播图表成功，结果：{@Result}", result);
                return result;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "添加轮播图表失败，命令参数：{@Command}", command);
                throw;
            }
        }

        /// <summary>
        /// 修改轮播图表
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<object>> UpdateCarouselChart(UpdateCarouselChartCommand command)
        {
            try
            {
                logger.LogInformation("开始修改轮播图表，命令参数：{@Command}", command);
                var result = await mediator.Send(command);
                logger.LogInformation("修改轮播图表成功，结果：{@Result}", result);
                return result;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "修改轮播图表失败，命令参数：{@Command}", command);
                throw;
            }
        }

        /// <summary>
        /// 逻辑删除轮播图表
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<APIResult<object>> DeleteCarouselChart(DeleteCarouselChartCommand command)
        {
            try
            {
                logger.LogInformation("开始删除轮播图表，命令参数：{@Command}", command);
                var result = await mediator.Send(command);
                logger.LogInformation("删除轮播图表成功，结果：{@Result}", result);
                return result;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "删除轮播图表失败，命令参数：{@Command}", command);
                throw;
            }
        }
    }
}
